using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MSRI.HelperClasses;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Xml;
using System.IO;
using Instedd.Labs.Geo.Library.Sms;
using Instedd.Labs.Geo.Library.Rss;
using Instedd.Labs.Geo.Library.Kml;

namespace Instedd.Labs.Geo.Library.Tests.Managers
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class RssManagerFixture
    {
        public RssManagerFixture()
        {}

        [TestMethod]
        public void EmptyMessageListCreatesAnEmptyRss()
        {
            RssManager manager = new RssManager();

            RssFormat rss = manager.CreateGeoRss(new SmsExtension[] { });

            Assert.IsNotNull(rss);
            Assert.AreEqual(1, rss.Channels.Count);
            Assert.AreEqual(0, rss.Channels[0].Items.Count);
        }

        [TestMethod]
        public void Test01()
        {
            RssManager manager = new RssManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland", "Tom", "444")
            };

            messages[0].Sms.Time = new DateTime(2000, 1, 7, 18, 30, 0);

            RssFormat rss = manager.CreateGeoRss(messages);

            Assert.AreEqual(1, rss.Channels[0].Items.Count);
            Assert.AreEqual("Tom", rss.Channels[0].Items[0].Title);
            Assert.AreEqual("<i>Last message at 18:30 (01-07-2000)</i><br/><b>Tom:</b> I'm at Kirkland<br/>", rss.Channels[0].Items[0].Description);
            Assert.AreEqual(new LatLong(47.68166999999999, -122.2075), rss.Channels[0].Items[0].LatLong);
        }

        //[TestMethod]
        //public void TwoMessagesWithDifferentLocationAndSameSenderNameGeneratesAPathBetweenLocations()
        //{
        //    KmlManager manager = new KmlManager();

        //    SmsExtension[] messages = new SmsExtension[]{ 
        //        new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.13099), "I'm at Redmond", "Tom", "444")
        //    };

        //    KmlFormat kml = manager.CreateKml(messages);

        //    Assert.AreEqual(1, kml.Folders[0].Paths.Count);
        //    Assert.AreEqual(2, kml.Folders[0].Paths[0].Coordinates.Count);
        //    Assert.AreEqual(new LatLong(47.68166999999999, -122.2075), kml.Folders[0].Paths[0].Coordinates[0]);
        //    Assert.AreEqual(new LatLong(47.67861000000001, - 122.13099), kml.Folders[0].Paths[0].Coordinates[1]);
        //}

        //[TestMethod]
        //public void TwoDifferentMessagesFromTheSameSenderNameAndLocationGeneratesOneSinglePlacemarkWithBothMessages()
        //{
        //    KmlManager manager = new KmlManager();

        //    SmsExtension[] messages = new SmsExtension[]{ 
        //        new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland.", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "And I'm going to Redmond.", "Tom", "444")
        //    };

        //    DateTime timestamp = new DateTime(2000, 1, 7, 12, 30, 0);
        //    messages[1].Sms.Time = timestamp;


        //    KmlFormat kml = manager.CreateKml(messages);

        //    Assert.AreEqual(1, kml.Folders[0].Placemarks.Count);
        //    Assert.AreEqual("Tom", kml.Folders[0].Placemarks[0].Name);
        //    Assert.AreEqual("<i>Last message at 12:30 (01-07-2000)</i><br/><b>Tom:</b> I'm at Kirkland.<br/><b>Tom:</b> And I'm going to Redmond.<br/>", kml.Folders[0].Placemarks[0].Description);
        //    Assert.AreEqual(new LatLong(47.68166999999999, -122.2075), kml.Folders[0].Placemarks[0].Location);
        //}

        //[TestMethod]
        //public void ValidLinkTemplateReturnsFormattedAnswerLink()
        //{
        //    KmlManager manager = new KmlManager();
        //    manager.LinkTemplate = @"http://localhost:51424/SMS.VE.Host.Web/AnswerSMS.aspx?SenderName={0}&amp;Message={1}&amp;Number={2}&amp;MessageId={3}";

        //    SmsExtension message = new SmsExtension(new SmsClass(), new LatLong(12.22, 33.44), "Message description", "Tom", "444");
        //    message.Sms.Number = "4444-5555";
        //    message.Sms.MessageID = new Guid("5a8fa480-0d36-42f6-8709-e15264a3dd98");

        //    Assert.AreEqual(" <a href=\"http://localhost:51424/SMS.VE.Host.Web/AnswerSMS.aspx?SenderName=Tom&amp;Message=Message description&amp;Number=4444-5555&amp;MessageId=5a8fa480-0d36-42f6-8709-e15264a3dd98\">Reply</a>", manager.GenerateReplyLink(message));
        //}

        //[TestMethod]
        //public void NullLinkTemplateReturnEmptyAnswerLink()
        //{
        //    KmlManager manager = new KmlManager();
        //    manager.LinkTemplate = null;

        //    SmsExtension message = new SmsExtension(new SmsClass(), new LatLong(12.22, 33.44), "Message description", "Tom", "444");

        //    Assert.AreEqual(string.Empty, manager.GenerateReplyLink(message));
        //}

        //[TestMethod]
        //public void EmptyLinkTemplateReturnEmptyAnswerLink()
        //{
        //    KmlManager manager = new KmlManager();
        //    manager.LinkTemplate = string.Empty;

        //    SmsExtension message = new SmsExtension(new SmsClass(), new LatLong(12.22, 33.44), "Message description", "Tom", "444");

        //    Assert.AreEqual(string.Empty, manager.GenerateReplyLink(message));
        //}

        //[TestMethod]
        //public void TwoDifferentMessagesFromTheDifferentSenderNameGeneratesTowFolders()
        //{
        //    KmlManager manager = new KmlManager();

        //    SmsExtension[] messages = new SmsExtension[]{ 
        //        new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland (Tom).", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland (John).", "John", "444")
        //    };

        //    KmlFormat kml = manager.CreateKml(messages);

        //    Assert.AreEqual(2, kml.Folders.Count);
        //    Assert.AreEqual("Tom", kml.Folders[0].Name);
        //    Assert.AreEqual("John", kml.Folders[1].Name);
        //    Assert.AreEqual("Tom", kml.Folders[0].Placemarks[0].Name);
        //    Assert.AreEqual("John", kml.Folders[1].Placemarks[0].Name);
        //    Assert.AreEqual(new LatLong(47.68166999999999, -122.2075), kml.Folders[0].Placemarks[0].Location);
        //    Assert.AreEqual(new LatLong(47.68166999999999, - 122.2075), kml.Folders[1].Placemarks[0].Location);
        //}

        //[TestMethod]
        //public void MessageFromDifferentLocationAtDifferentTimesCreatesPlacemarksUsingDifferentStylesBasedOnTheArrivedTime()
        //{
        //    KmlManager manager = new KmlManager();

        //    SmsExtension[] messages = new SmsExtension[]{ 
        //        new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075),  "I'm at Kirkland", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.13099), "I'm at Redmond", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.10099), "I'm at Redmond", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.11099), "I'm at Redmond", "Tom", "444")
        //    };

        //    messages[0].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 60, 0));
        //    messages[1].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 45, 0));
        //    messages[2].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 25, 0));
        //    messages[3].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 10, 0));


        //    KmlFormat kml = manager.CreateKml(messages);

        //    Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory4Style, kml.Folders[0].Placemarks[0].StyleUrl);
        //    Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory3Style, kml.Folders[0].Placemarks[1].StyleUrl);
        //    Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory2Style, kml.Folders[0].Placemarks[2].StyleUrl);
        //    Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory1Style, kml.Folders[0].Placemarks[3].StyleUrl);
        //}

        //[TestMethod]
        //public void MessageFromDifferentLocationAtDifferentTimesCreatesLatestPlacemarkInThreadWith100PercentOpaqueNotMatterTheTimeItArrived()
        //{
        //    KmlManager manager = new KmlManager();

        //    SmsExtension[] messages = new SmsExtension[]{ 
        //        new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075),  "I'm at Kirkland", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.13099), "I'm at Redmond", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.10099), "I'm at Redmond", "Tom", "444"),
        //        new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.11099), "I'm at Redmond", "Tom", "444")
        //    };

        //    messages[0].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 100, 0));
        //    messages[1].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 80, 0));
        //    messages[2].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 70, 0));
        //    messages[3].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 50, 0));


        //    KmlFormat kml = manager.CreateKml(messages);

        //    Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory4Style, kml.Folders[0].Placemarks[0].StyleUrl);
        //    Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory4Style, kml.Folders[0].Placemarks[1].StyleUrl);
        //    Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory4Style, kml.Folders[0].Placemarks[2].StyleUrl);
        //    Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory1Style, kml.Folders[0].Placemarks[3].StyleUrl);
        //}
    }
}
